<template>
    <div class="box_trivia"
         v-if="skeleton"
         @mouseover="shaking=!shaking"
         @mouseout="shaking=!shaking"
         :class="{'turnOver_':turnOver,'shaking':shaking, 'global_love':$store.state.nowTime==='moon'}">
        <div class="content">
            <img :src="trivia_content.triviaImage" alt=""
                 class="img_"
                 :class="{'global_love_back':$store.state.nowTime==='moon'}">
            <p class="content_body" v-html="trivia_content.content" ></p>
        </div>
        <div class="bottom">
            <span class="time">{{ trivia_content.time }}</span>
            <div class="one" @click="next" @mouseout="">
                <img src="/img/common/qiehuan.png" alt="" title="下一条">
            </div>
        </div>
    </div>
    <div v-else-if="trivia_content.content!==''" class="box_trivia"  >
        <div class="circle_box">
            <CircleLoading></CircleLoading>
        </div>
    </div>
  <div v-else class="trivia_noContent ">
      <h2 :class="{'global_font':$store.state.nowTime==='moon'}">暂无信息！</h2>
  </div>
</template>

<script>
import api from "@/api";
import CircleLoading from "@/components/PublicComponents/CircleLoading.vue";

export default {
    name: "LoveEveryday",
    components: {CircleLoading},
    data() {
        return {
            index: 0,
            turnOver: false,
            shaking: false,
            trivia: [{
                id: "",
                content: "",
                triviaImage: "",
                time: ""
            }],
            index_: "",
            trivia_content: {
                id: "",
                content: "",
                triviaImage: "",
                time: ""
            },
            skeleton:false,
        }
    },
    mounted() {
        api.getTrivia().then((res) => {
            if(res.data.data.length>0){
                this.trivia = res.data.data;
                this.random(this.trivia);
            }
        })
    },
    watch: {
        trivia_content(newValue,oldValue){
              if(newValue !== ""){
                  setTimeout(()=>{
                      this.skeleton = true;
                  },100)
              }
        }
    },
    methods: {
        random(item) {
            this.index_ = Math.floor(Math.random() * ((item.length - 1) - 0 + 1));
            this.trivia_content = this.trivia[this.index_]
        },
        next() {
            if (this.turnOver) {
                return
            }
            this.turnOver = !this.turnOver
            setTimeout(() => {
                this.turnOver = !this.turnOver
                this.shaking = true
                this.index_ += 1;
                if (this.index_ === this.trivia.length) {
                    this.index_ = 0;
                }
                this.trivia_content = this.trivia[this.index_]
            }, 800)

        },

    }
}
</script>

<style scoped>

.circle_box{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    border-radius:5px;
    box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    background: rgba(246, 247, 248, 0.7);

}
.box_trivia {
    position: relative;
    margin: auto;
    width: 500px;
    height: 420px;
    padding: 10px;
    border-radius: 4px;
    transition: all 1s ease-in-out;
    background: rgba(255,255,255,0.6);
    box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.2), 0 2px 5px 0 rgba(0, 0, 0, 0.19);
    color: #8f8c8c;
}

.shaking {
    animation: shaking_ 1.5s ease-in-out 0s infinite forwards;
}

@keyframes shaking_ {
    0% {
        transform: translateY(0px);
    }
    50% {
        transform: translateY(-10px);
    }
    100% {
        transform: translateY(0px);
    }
}

.content {
    word-break: break-all;
    width: 100%;
    height: 95%;
    letter-spacing: 1px;
    overflow: auto;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

}
.content .img_ {
    float: left;
    max-width: 400px;
    margin-right: 10px;
    margin-bottom: 5px;
    aspect-ratio: 14/9;
}

.bottom {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
}

.time {
    display: flex;
    align-items: center;
    padding-left: 5px;
    height: 100%;
    padding-top: 5px;
}

.one {
    cursor: pointer;
    border-radius: 50%;
    padding: 5px;
    background: rgba(137, 231, 243, 0.4);
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all .3s ease 0s;
    margin-right: 5px;
    margin-bottom: 5px;
    transform: rotate(-360deg);
}

.one img {
    margin: auto;
    width: 20px;
    height: 20px;
    border-radius: 50%;

}

.one:hover {
    opacity: .9;
    color: #3586f1;
    animation: revolve 0.3s ease-in-out 0s 1 forwards;
}
.content_body{
    min-height: 100px;
    width: 100%;
    padding: 0 !important;
}

.content::-webkit-scrollbar {
    width: 5px;
}

.content::-webkit-scrollbar-thumb {
    border-radius: 1px;
    background: #f1e2c3;
}

.content::-webkit-scrollbar-track {
    border-radius: 0;
}
@keyframes revolve {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}

.turnOver_ {
    animation: turnOver 1.9s ease-in-out 0s 1 forwards;
}

@keyframes turnOver {
    0% {
        transform: rotateX(0deg);
    }
    50% {
        transform: rotateX(90deg);
    }
    100% {
        transform: rotateX(0deg)
    }
}
.trivia_noContent{
    height: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #8f8c8c;
}
@media (min-width: 0) and (max-width: 600px) {
    .box_trivia {
        bottom: auto;
        top: 2%;
        width: 360px;
        height: 500px;
    }

    .content .img_ {
        width: 280px;
    }
}

</style>